package com.lfy.modules.monitor.service.impl;

import com.lfy.modules.common.config.sys.DictionariesKey;
import com.lfy.modules.common.config.sys.SysDictionaries;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.config.thread.MyLinkedBlockingQueue;
import com.lfy.modules.monitor.ThreadPoolMontitor;
import com.lfy.modules.monitor.domain.vo.ThreadInformationVO;
import com.lfy.modules.monitor.service.ThreadPoolDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author lfy
 * @description 针对表【thread_pool_detail】的数据库操作Service实现
 * @createDate 2024-04-02 18:33:56
 */
@Service
@Slf4j
public class ThreadPoolDetailServiceImpl
        implements ThreadPoolDetailService {
    @Resource
    private ThreadPoolExecutor mainThreadPool;

    @Override
    public void updateThreadPoolQueueSize(Integer queueSize) {
        MyLinkedBlockingQueue queue = (MyLinkedBlockingQueue) mainThreadPool.getQueue();
        queue.setCapacity(queueSize);
        mainThreadPool.execute(() -> {
            try {
                SysDictionaries.updateValueInMysql(DictionariesKey.SYS_THREAD_QUEUE,String.valueOf(queueSize));
            } catch (Exception e) {
                log.error("in com.lfy.monitor.service.impl.ThreadPoolDetailServiceImpl.updateThreadPoolQueueSize",e);
            }
        });
    }

    @Override
    public void updateThreadPoolMaxSize(Integer maxSize) {
        mainThreadPool.setMaximumPoolSize(maxSize);
        mainThreadPool.execute(() -> {
            try {
                SysDictionaries.updateValueInMysql(DictionariesKey.SYS_THREAD_MAX,String.valueOf(maxSize));
            } catch (Exception e) {
                log.error("in com.lfy.monitor.service.impl.ThreadPoolDetailServiceImpl.updateThreadPoolMaxSize ",e);
            }
        });
    }

    @Override
    public void updateThreadPoolCoreSize(Integer coreSize) {
        mainThreadPool.setCorePoolSize(coreSize);
        mainThreadPool.execute(() -> {
            try {
                SysDictionaries.updateValueInMysql(DictionariesKey.SYS_THREAD_CORE,String.valueOf(coreSize));
            } catch (Exception e) {
                log.error("in com.lfy.monitor.service.impl.ThreadPoolDetailServiceImpl.updateThreadPoolCoreSize ",e);
            }
        });
    }

    @Override
    public void updateThreadAlarm(Integer openAlarm) {
        ThreadPoolMontitor.setOpenAlarm(openAlarm);
        mainThreadPool.execute(() -> {
            try {
                SysDictionaries.updateValueInMysql(DictionariesKey.SYS_THREAD_ALARM,String.valueOf(openAlarm));
            } catch (Exception e) {
                log.error("in com.lfy.monitor.service.impl.ThreadPoolDetailServiceImpl.updateThreadAlarm 原因 thread_pool_detail 数据不存在");
            }
        });
    }

    @Override
    public R<ThreadInformationVO> getInformation() {
        ThreadInformationVO threadInformationVO = new ThreadInformationVO();
        threadInformationVO.setCoreSize(mainThreadPool.getCorePoolSize());
        threadInformationVO.setMaxSize(mainThreadPool.getMaximumPoolSize());
        MyLinkedBlockingQueue queue = (MyLinkedBlockingQueue) mainThreadPool.getQueue();
        threadInformationVO.setQueueSize(queue.getCapacity());
        threadInformationVO.setOpenAlarm(ThreadPoolMontitor.getOpenAlarm());
        return R.success(threadInformationVO);
    }
}




